#!/usr/bin/perl 

use strict;

use DBI;
use CGI;
use TTraker;

print_header;
my $dbh;
initialize_dbi;

my $form_ref = parse_input();
my %FORM_DATA = %$form_ref; 
my $id = $FORM_DATA{'empID'};

if ($id =~ m/^\d+$/) {
	$id = "invalid_entry";
}	

# convert $id to uppercase and trim any spaces at head and tail
$id =~ tr/a-z/A-Z/;
$id =~ s/^\s*//;
$id =~ s/$\s*//;
$id =~ s/\s+//g;

my $sent_from = $ENV{'REMOTE_ADDR'};

my ($name, $log_status, $log_status_ref, $log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, 
	$dow, $month, $day, $year, $time, $nod, $noh, $date, $pay_count, $first, $last, $job, $num, $loc);

my $now = get_now();
my $today = get_today;
my $code = "LOGIN";

open (LOG, ">> $log")
		or die "CAN NOT OPEN $log";	
print LOG "$now,$sent_from,trak_01_emp_log_in.pl,$id\n";
close LOG;


# find out if there is a match for the employee id in the employees table (hopefully only one!)
my $head_count = get_employee_count ($id);
# print "getting now!<br/>";
# print "NOW IS: $now AND TODAY IS: $today<br/>";
if ( $head_count == 1 ) { # this is good - one match found in employees table
	my $array_ref = get_employee_info ($id);
	($num, $first, $last, $job, $loc) = @$array_ref;
	$now = get_local_time($loc);
	start_system_time ($num, $code, $now);
	$name = "$first" . " $last";
    if ( $job eq "WHS" || $job eq "CSR" || $job eq "TMP" ) { 
#		find out if the employee is logged in and how many times (hopefully 0 or 1!)
  		my $logged_in = get_log_in_count ($num);
#		print "LOGGED IN = $logged_in <br/>";
#		if not logged in insert entry in logged_in table and also the day_events table
		if ( $logged_in == 0 ) {
			log_employee_in ($num, $now);
#			normally there is not an open job when we are logged out EXCEPT for LUNCH!
			pause_current_job ($num, $now) ;
#			create an idle event to fill in the time from log_in till activity starts
			create_idle_event ($num, $now);
		} elsif ($logged_in == 1) {
			$log_status = 0;
			$log_status_ref = log_in_get_status ($num, $now); # returns an array of time info regarding the current log in
			($log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date) = @$log_status_ref;
			$noh = sprintf "%.1f", $log_elapsed/60;
			if ($log_elapsed > 600 && $job_elapsed > 300 || $log_elapsed > 600 && $job_elapsed eq "" ) {
				$log_status = 1;
			}
		}
# 		print the activity selection page for the browser
		if ( $log_status == 0 ) {
#			find out if the employee is logged in for payroll hours again (hopefully 0 or 1!) 
    		$pay_count = get_payroll_count ($num, $today); 
#    		print "PAYCOUNT RETURNED: $pay_count<br/>";
#			if no payroll entry for today exists create it.
			if ( $pay_count == 0 ) {
	    		create_payroll_event ($num, $today);
			}
			if ( $job eq "CSR") {
				csr_activity_screen_from_pm ($num, $name, $job, $loc,"");
			} else {
				activity_screen_from_pm ($num, $name, $job, $loc, "");			
			}
	    } elsif ( $log_status == 1 ) {
	    	log_in_status_issue ($num, $name, $job, $loc, $log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date, $noh);
	    }	
   } elsif ( $job eq "SUP") {
# 		print the supervisors activity selection page for the browser
		sup_activity_screen_from_pm ($num, $name,$job, $loc, "");
   } elsif ( $job eq "ADM") {
# 		print the administrators selection page for the browser
		adm_activity_screen_from_pm ($num, $name,$job, $loc, "");
   } elsif ( $job eq "PAY") {
# 		print the payroll administrators selection page for the browser
		pay_activity_screen_from_pm ($num,$name,$job,$loc, "");
	} 
} elsif ( $head_count == 0 ) {
   # no employee number found print a new log-in screen
	my $msg = "Employee ID $id not found in Data Base\!";
    log_in_screen_from_pm($msg)
} else {
   # we have a serious problem - more than one employee number found!
   trak_01_emp_log_in_more_than_one_number(); 
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub log_in_status_issue
{
$num = $_[0];
$name = $_[1];
$job = $_[2];
$loc = $_[3];
$log_elapsed = $_[4];
$job_elapsed = $_[5];
$log_start = $_[6];
$job_start = $_[7];
$log_hrs = $_[8];
$job_hrs = $_[9];
$dow = $_[10];
$month = $_[11];
$day = $_[12];
$year = $_[13];
$time = $_[14];
$nod = $_[15];
$date = $_[16];
$noh = $_[17];

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_01_emp_log_in_staus_issue</title>
</head>
<body onload='resizeWide();'>
<h2><center>${banner}</center></h2>
<hr/>
<h3>Log In Issue</h3>

<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_re_log_in_time_issue.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>
<input type="hidden" name=log_elapsed value='$log_elapsed'/>
<input type="hidden" name=job_elapsed value='$job_elapsed'/>
<input type="hidden" name=log_start value='$log_start'/>
<input type="hidden" name=job_start value='$job_start'/>
<input type="hidden" name=log_hrs value='$log_hrs'/>
<input type="hidden" name=job_hrs value='$job_hrs'/>
<input type="hidden" name=dow value='$dow'/>
<input type="hidden" name=month value='$month'/>
<input type="hidden" name=day value='$day'/>
<input type="hidden" name=year value='$year'/>
<input type="hidden" name=time value='$time'/>
<input type="hidden" name=nod value='$nod'/>
<input type="hidden" name=date_start value='$date'/>
<input type="hidden" name=noh value='$noh'/>

<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<b id="warn">
	You have been logged in since<br/>
	$dow $month $day, $year @ $time<br/>
	That is over $noh hours! What is the reason?<br/><br/>
</b>
<label><input type="radio" name="reason" value="1" checked="checked"/> &nbsp&nbsp I Forgot to log out, I left work at:</label><br/><br/>
<label>&nbsp&nbsp&nbsp&nbsp</label>
<label><input type="text" size="1" name="hrs" /></label>
<label><input type="text" size="1" name="min" /></label>
<label><input type="radio" name="ampm" value="0" />am</label>
<label><input type="radio" name="ampm" value="12" CHECKED />pm &nbsp&nbsp on $dow.</label>
<br/><br/><br/>
<input type="radio" name="reason" value="2"/> &nbsp&nbsp I have been working continuously during this time.<br/>
<br/>
<input type='submit' value='Log Activity'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
<b id="info">
	Note: If you forgot to log out the current log in will be closed <br/>
	and a new log in will be created at the current date and time.
</form>
</body>
</html>

ENDHEADER
		
}


sub trak_02_activity_selection
{

my $num = $_[0];
my $name = $_[1];
my $job = $_[2];
my $loc = $_[3];

my $msg = get_message ($num);

my $dis_ref = get_disabled_list ($num);
my @dis = @$dis_ref;

my $col_ref = get_dimmed_list ($num);
my @col = @$col_ref;

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>

<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Start Job</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> Close Job (do not log tasks)</$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> Log Tasks</$col[3]><br/>
<input $dis[3] type="radio" name="activity" value="8"/>&nbsp<$col[3]> Log Batch Tasks</$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> Continue Job</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Lunch</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Break</$col[6]><br/>
<input type="radio" name="activity" value="7"/><$col[7]>&nbsp Leave Work</$col[7]><br/>
<br/>
<input type='submit' value='Log Activity'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}

sub trak_02_sup_activity_selection
{

my $week_ref = get_payroll_review_weeks;
my @weeks = @$week_ref;
# print "HEY $weeks[0][0] - $weeks[0][1] : $weeks[1][0] - $weeks[1][1] : $weeks[2][0] - $weeks[2][1] <br/>";
my $msg = & get_message ($num);
# my @dis = & get_disabled_list ($num);
# my @col = & get_dimmed_list ($num);
my @dis = ("", "", "", "", "", "", "", "","","disabled=\"disabled\"");
my @col = ("dis","en","en","en","en","en","en","en", "en", "dis"); 

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_sup_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_sup_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input $dis[1]type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Confirm Daily Payroll</$col[1]><br/>
<input $dis[2]type="radio" name="activity" value="$weeks[0][0]"/>&nbsp<$col[2]> Review Payroll - $weeks[0][1] </$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="$weeks[1][0]"/>&nbsp<$col[3]> Review Payroll - $weeks[1][1] </$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="$weeks[2][0]"/>&nbsp<$col[4]> Review Payroll - $weeks[2][1]</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Add New Temp Employee</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Who is logged in?</$col[6]><br/>
<input $dis[7] type="radio" name="activity" value="7"/>&nbsp<$col[7]> Enter Data from Time Cards</$col[6]><br/>
<input $dis[8] type="radio" name="activity" value="8"/>&nbsp<$col[8]> Modify Tasks for Individulal Jobs </$col[7]><br/>
<input $dis[9] disabled="disabled" type="radio" name="activity" value="9"/>&nbsp<$col[9]> TBA</$col[9]><br/>

<br/>
<input type='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER
}

sub trak_02_adm_activity_selection_OLD
{

my $msg = & get_message ($num);
# my @dis = & get_disabled_list ($num);
# my @col = & get_dimmed_list ($num);
my @dis = ("", "", "", "", "", "", "", "","disabled=\"disabled\"");
my @col = ("dis","en","en","en","en","en","en","en"); 

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_adm_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_adm_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Adjust Client Standard Time/Rate</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> Adjust Master Standard Time</$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> Add New Client</$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> Add Client Task</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Add Master Task</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Update Client Information</$col[6]><br/>
<input $dis[6] type="radio" name="activity" value="7"/>&nbsp<$col[7]> Reports<br/>
<br/>
<input type='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER
}

sub trak_02_pay_activity_selection
{

my $week_ref = get_payroll_review_weeks;
my @weeks = @$week_ref;
# print "HEY $weeks[0][0] - $weeks[0][1] : $weeks[1][0] - $weeks[1][1] : $weeks[2][0] - $weeks[2][1] <br/>";
my $msg = & get_message ($num);
# my @dis = & get_disabled_list ($num);
# my @col = & get_dimmed_list ($num);
my @dis = ("", "", "", "", "", "", "", "","disabled=\"disabled\"");
my @col = ("dis","en","en","en","en","en","en","en","en","en","dis"); 

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_pay_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_pay_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input $dis[1] type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Generate Payroll Table</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> Generate Agency Hours Table<br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> Add New Permanent Employee </$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> Add New Temp Employee</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Modify Employee Data</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Close Payroll Dates</$col[5]><br/>
<input $dis[7] type="radio" name="activity" value="7"/>&nbsp<$col[6]> Open Payroll Dates</$col[5]><br/>
ENDHEADER

# <input $dis[8] disabled="disabled" type="radio" name="activity" value="8"/>&nbsp<$col[6]> TBA</$col[5]><br/>
print <<ENDHEADER;
<br/>
<input type='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER
}

sub trak_01_emp_log_in_no_number
{
print <<ENDHEADER;

<html>
<head>
<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
<script type="text/javascript" src="${js_ref}trak.js"></script>
<title>trak_01_emp_num_log_in_no_number</title>
</head>
<body onload="startTime()">
<h2><center>${banner}</center></h2>
<hr/>
<div id="clock"></div>
<div id="box1">
   &nbsp&nbsp&nbsp&nbsp&nbsp Employee ID: <br/><br/>
</div> 
<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_01_emp_log_in.pl" method="post">
   <input type="text" name=empID />&nbsp&nbsp<br/><br/><br/>
   <input type="submit" value="Log Into system">
   <br/><br/><br/>
   <b id="warn">Employee ID $id not found in Data Base!</b>
</form>

</body>
</html>

ENDHEADER
}

sub trak_01_emp_log_in_more_than_one_number
{
print <<ENDHEADER;

<html>
<head>
<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
<script type="text/javascript" src="${js_ref}trak.js"></script>
<title>trak_01_emp_num.html NO NUM</title>
</head>
<body onload="startTime()">
<h2><center>${banner}</center></h2>
<hr/>
<div id="clock"></div>
<div id="box1">
   &nbsp&nbsp&nbsp&nbsp&nbsp Employee ID: <br/><br/>
</div> 
<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_01_emp_log_in.pl" method="post">
   <input type="text" name=empID />&nbsp&nbsp
   <input type="submit" value="Log Into system">
   <br/><br/><br/>
   <b id="warn">More than one entry found for Employee Number $num - contact administrator!</b>
</form>
</body>
</html>

ENDHEADER
}

